<?php

if (!defined('IN_VIEWPROFILE')) die(basename(__FILE__));

if (!$profiledata['user_id'] || $profiledata['user_id'] == ANONYMOUS)
{
	message_die(GENERAL_ERROR, $lang['NO_USER_ID_SPECIFIED']);
}

$seeding = $leeching = $releasing = array();

$profile_user_id = intval($profiledata['user_id']);
$current_time = (isset($_GET['time']) && $_GET['time'] == 'all') ? 0 : TIMENOW;

// Get username
if (!$username = $profiledata['username'])
{
	message_die(GENERAL_ERROR, 'Tried obtaining data for a non-existent user');
}

if ($profile_user_id == $userdata['user_id'])
{
	$template->assign_vars(array(
		'EDIT_PROF'      => true,
		'EDIT_PROF_HREF' => "profile.php?mode=editprofile",
	));
}
else
{
	$template->assign_vars(array('EDIT_PROF' => false));
}

// Auth
$excluded_forums_csv = $user->get_excluded_forums(AUTH_VIEW);
$not_auth_forums_sql = ($excluded_forums_csv) ? "
	AND f.forum_id NOT IN($excluded_forums_csv)
	AND f.forum_parent NOT IN($excluded_forums_csv)
" : '';

// Get users active torrents
$sql = 'SELECT f.forum_id, f.forum_name, t.topic_title, sn.seeders, sn.leechers, tor.size, tor.tor_type, tor.tor_status, tr.*
	FROM '. BB_FORUMS .' f, '. BB_TOPICS .' t, '. BB_BT_TRACKER .' tr, '. BB_BT_TORRENTS ." tor
	LEFT JOIN ". BB_BT_TRACKER_SNAP ." sn ON(tor.topic_id = sn.topic_id)
	WHERE tr.user_id = $profile_user_id
		AND tr.topic_id = tor.topic_id
		AND tor.topic_id = t.topic_id
		AND t.forum_id = f.forum_id
			$not_auth_forums_sql
	GROUP BY tr.topic_id
	ORDER BY f.forum_name, t.topic_title";

if (!$result = DB()->sql_query($sql))
{
	message_die(GENERAL_ERROR, 'Could not query users torrent profile information', '', __LINE__, __FILE__, $sql);
}

if ($rowset = @DB()->sql_fetchrowset($result))
{
	DB()->sql_freeresult($result);
	$rowset_count = count($rowset);

	for ($i=0; $i<$rowset_count; $i++)
	{
		if ($rowset[$i]['releaser'] and $rowset[$i]['seeder'])
		{
			$releasing[] = $rowset[$i];
		}
		else if ($rowset[$i]['seeder'])
		{
			$seeding[] = $rowset[$i];
		}
		else
		{
			$leeching[] = $rowset[$i];
		}
	}
	unset($rowset);
}

if ($releasing_count = count($releasing))
{
    $template->assign_block_vars('released', array());
    $releas_size = 0;
    for ($i=0; $i<$releasing_count; $i++)
    {
        $releas_size = $releas_size + $releasing[$i]['size'];
    }
}
else
{
    $template->assign_block_vars('switch_releasing_none', array());
}

if ($seeding_count = count($seeding))
{
    $template->assign_block_vars('seed', array());

    $seed_size = 0;
    for ($i=0; $i<$seeding_count; $i++)
    {
        $seed_size = $seed_size + $seeding[$i]['size'];
    }
}
else
{
    $template->assign_block_vars('switch_seeding_none', array());
}
if ($leeching_count = count($leeching))
{
    $template->assign_block_vars('leech', array());

    $leech_size = 0;
    for ($i=0; $i<$leeching_count; $i++)
    {
        $leech_size = $leech_size + $leeching[$i]['size'];
    }
}
else
{
    $template->assign_block_vars('switch_leeching_none', array());
}

$template->assign_vars(array(
    'USERNAME'  => $username,
    'L_RELEASINGS'=> '<b>'. $lang['RELEASING'] .'</b>'. (($releasing_count) ? "<br />[ <b>$releasing_count</b> ]<br />" . '<nobr>('.humn_size($releas_size).')</nobr>' : ''),
    'L_SEEDINGS'  => '<b>'. $lang['SEEDING'] .'</b>'. (($seeding_count) ? "<br />[ <b>$seeding_count</b> ]<br />" . '<nobr>('.humn_size($seed_size).')</nobr>' : ''),
    'L_LEECHINGS' => '<b>'. $lang['LEECHING'] .'</b>'. (($leeching_count) ? "<br />[ <b>$leeching_count</b> ]<br />" . '<nobr>('.humn_size($leech_size).')</nobr>' : ''),

    'RELEASED_ROWSPAN' => ($releasing_count) ? 'rowspan="'. ($releasing_count + 1) .'"' : '',
    'SEED_ROWSPAN'    => ($seeding_count) ? 'rowspan="'. ($seeding_count + 1) .'"' : '',
    'LEECH_ROWSPAN'    => ($leeching_count) ? 'rowspan="'. ($leeching_count + 1) .'"' : '',
));

if ($releasing_count || $seeding_count || $leeching_count) 
{
    $template->assign_vars(array(
        'ACTIV_RELEASED1'=> true,
        'ACTIV_RELEAS'=> ' ('. $lang['RELEASING'] .': <b>'.$releasing_count.'</b> - '.@humn_size($releas_size).', '. $lang['SEEDING'] .': <b>'.$seeding_count.'</b> - '.@humn_size($seed_size).', '. $lang['LEECHING'] .': <b>'.$leeching_count.'</b> - '.@humn_size($leech_size).')',
    ));
}
else 
{
    $template->assign_vars(array(
        'ACTIV_RELEASED1'=> false,
        'ACTIV_RELEAS'=> ': <b>'. $lang['NO'] .'</b>',
    ));
}

$template->assign_vars(array('SHOW_SEARCH_DL' => false));

if (!IS_USER || $profile_user_id == $userdata['user_id'])
{
	$page_cfg['dl_links_user_id'] = $profile_user_id;
}
